import p from "../../utils/platform"
import api from "../../common/api"
import { mathchSearchValue, debounce } from '../../utils/utilsNew';

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    hotList: [],
    isHideHistory: false,
    placeholder: "",
    searchTextValue: "",
    backUrl: "",
    appKey: "", //需要保存的搜索关键字的字段
    houseList: [],
    page: 1,
    pageSize: 99,
    loading: false,
    focus: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.handleInputChange = debounce(this.handleInputChange.bind(this), 1000)
    this.data.appKey = options.appKey
    if (options.placeholder) {
      this.setData({
        placeholder: options.placeholder
      })
    }

    if (options.searchTextValue) {
      this.setData({
        searchTextValue: options.searchTextValue
      })
      this.handleInputChange()
    }

    // 获取缓存搜索
    let _data = wx.getStorageSync(`_wx_${this.data.appKey}`) || ""
    if (_data) {
      this.setData({
        list: _data.split(",")
      })
    }

  },

  handleClear() {
    p.showModal({
        content: "确认清空历史搜索记录？",
      })
      .then((res) => {
        if (res.confirm) {
          this.setData({
            list: []
          })

          p.removeStorageSync(`_wx_${this.data.appKey}`)
        }
      })
  },

  handleToggle() {
    this.setData({
      isHideHistory: !this.data.isHideHistory
    })
  },

  handleInputChange(e) {
    console.log('e=====' , e)
    this.setData({
      loading: true
    })
    if(e){
      this.setData({
        searchTextValue: e.detail.value
      })
      if(e.detail.value == ''){
        this.setData({
          houseList: [],
          loading: false,
        })
        return
      }
    }
    api.likeResblockName({
      keyWord: this.data.searchTextValue,
      page: this.data.page,
      pageSize: this.data.pageSize,
    }, res => {
      let rev = res.data.content.rows;
      this.mixSearch(rev)
    })
  },

  handleBack() {
    p.navigateBack()
  },

  hanldeHistoryItem(e) {
    const name = e.target.dataset.name||e.currentTarget.dataset.name
    if (e && e.currentTarget && e.currentTarget.dataset && e.currentTarget.dataset.type) {
      app.globalData.searchType = e.currentTarget.dataset.type
    }
    setTimeout(() => {
      this.setData({
        searchTextValue: name
      })

      setTimeout(() => {
        this.handleConfirm()
      }, 500);
    }, 100)
  },

  handleConfirm(e) {
    const newValue = this.data.searchTextValue
    if (e && e.currentTarget && e.currentTarget.dataset && e.currentTarget.dataset.type) {
      app.globalData.searchType = e.currentTarget.dataset.type
    }
    if (!newValue) {
      return p.showToast({
        title: '请输入搜索词',
        icon: 'none',
      })
    }
    app.globalData[this.data.appKey] = newValue
    let _data = wx.getStorageSync(`_wx_${this.data.appKey}`) || ""
    let list = _data ? _data.split(",") : []

    let index = list.indexOf(newValue)
    if (index > -1) {
      list.splice(index, 1)
    }
    list.unshift(newValue)
    list.length = Math.min(list.length, 20)

    wx.setStorageSync(`_wx_${this.data.appKey}`, list.join(","))

    p.navigateBack()
  },
  mixSearch(res){
    let content = res;
    content.forEach((item)=>{
      item.contentList = mathchSearchValue(item.resblockName, this.data.searchTextValue)
    })
    console.log(content, 999)
    this.setData({
      houseList: content,
      loading: false
    })
  },
  hanldeClearSearchValue(){
    this.setData({
      loading: false,
      focus: false
    }, ()=> {
      this.setData({
        searchTextValue: '',
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

})